var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var http = require("helper/http");
var mallUrl = require("url/mall");
var helperOpen = require("helper/open");

var shopId = null;
var shopAddress = null;
var dingweiX,dingweiY

//获取上一个页面传入的参数
page.on("loaded", function(){
	var params = page.getData();
	shopId = params.id;
	shopAddress = params.address;
	addressLabelText.text = shopAddress;
});

//返回键
page.on("back", function() {
    app.closePage();
});

var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
ui(addheader + ".labelTitle").text = "修改店铺地址";
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage();
});

//保存
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "保存";
headerRight_lableNext.visible = true;
headerRight.on("touch", function() {
	page.hideKeyboard();
	submit();
});

var linearLayout_main = ui("do_LinearLayout_main");
/** -------------------add UI start ---------------------------**/
//设置活动地址
var addAddressLable = linearLayout_main.add("addressLable","source://view/reuse/form/mutiInput2.ui", 0, 0);
var addressLable = ui("addressLable");
hashdata.addData({
	"textTitle" : '地址',
	"imgForward":"source://image/menu/near.png",
	"inputType":"ASC",
	"hint":""
});
addressLable.bindData(hashdata);
addressLable.refreshData();
var addressLabelText=ui(addAddressLable+".textInput");
ui(addAddressLable + ".leftAlayout").on("touch",function() {
	helperOpen.open("source://view/area/map.ui");
});


var addLine = linearLayout_main.add("line", "source://view/reuse/form/line.ui",0,0);
ui("line").refreshData();
/** -------------------add UI end ---------------------------**/
page.on("result", function(data) {
	if(null!=data.longitude){//从打开地图定位后回来
		dingweiX = data.longitude;
		dingweiY = data.latitude;
		if(null!=data.address){
			addressLabelText.text=data.address;
		}
	}
});
function submit(){
	if (dingweiX=='' || dingweiX == null) {
		nf.toast("亲，请定位地址。");
		return;
	}
	if(addressLabelText.text=='' || addressLabelText.text==null){
		nf.toast("亲，请填写详细地址。");
		return;
	}
	var jsonData = {};
	jsonData.x = dingweiX;
	jsonData.y =dingweiY;
	jsonData.id = shopId;
	jsonData.address = addressLabelText.text;
	http.post(mallUrl.url.shop.updateAddressV1, jsonData, function(data) {
		//nf.toast("修改地址成功.");
		app.fire("fireShopUpdate",{"address":data.mallShop.address,"dingweiX":dingweiX,"dingweiY":dingweiX});
		app.closePage();
	});
};